package org.ruanjj.multithread;

public class WindowTest2 {
    public static void main(String[] args) throws InterruptedException {
        SaleTickets2 saleTickets = new SaleTickets2();
        Thread t1 = new Thread(saleTickets,"售票窗口1");
        t1.start();
        Thread t2 = new Thread(saleTickets,"售票窗口2");
        t2.start();
        Thread t3 = new Thread(saleTickets,"售票窗口3");
        t3.start();
        Thread t4 = new Thread(saleTickets,"售票窗口4");
        t4.start();

    }
}

class SaleTickets2 implements Runnable{
    private int tickets = 100;

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if(!show()){
                break;
            }
        }
    }


    //同步方法
    public synchronized boolean show(){
        if(tickets > 0){
            System.out.println(Thread.currentThread().getName() + "售票" + tickets);
            tickets--;
        }else{
            return false;
        }
        return true;
    }
}

